package cn.zhp.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import tk.mybatis.spring.annotation.MapperScan;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

//import org.mybatis.spring.annotation.MapperScan;

@Configuration
/**
 * 1.spring管理数据源
 * 2.spring管理SqlSessionFactoryBean
 * 3.扫描Mapper接口
 *
 *
 * druid性能监控：
 * 1.在数据源配置中修改添加filter
 * 2.设置druid的性能监控页面  通过其提供的servlet实现
 * 3.设置其他拦截监控（spring）
 */
@MapperScan(basePackages = "cn.zhp.mapper")  //扫描数据访问接口，创建代理子类 //替换为tkmapper的注解
@Import(SpringService.class)
public class SpringMybatis {

    @Bean
    public DruidDataSource getDataSource(){

        DruidDataSource dataSource = null;
        Properties properties = new Properties();
        try {
            properties.load(SpringMybatis.class.getClassLoader().getResourceAsStream("db.properties"));
            dataSource = new DruidDataSource();
            //自动加载配置文件中的数据 根据Properties对象的getProperties("druid.xxx")
            dataSource.configFromPropety(properties);
            dataSource.setFilters("stat,wall,log4j2");//设置druid的监控filter
        } catch (IOException e) {
            System.out.println("出现IO异常，检查文件加载");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }

    /*创建sqlSessionFactoryBean  替代  SQL Session Factory 用于设置mybatis的全局配置 和创建sqlSession*/
    @Bean
    public SqlSessionFactoryBean getFactoryBean(DruidDataSource druidDataSource) {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(druidDataSource);//设置数据源
        //设置mybatis原主配置文件信息
//        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        //替换为tkmapper提供的配置类
        tk.mybatis.mapper.session.Configuration configuration = new tk.mybatis.mapper.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);//数据库下划线命名到驼峰命名自动转换  如果是tkmapper的自带dao方法可以自动实现转换 自定义的方法才需要设置转换

        factoryBean.setConfiguration(configuration);
        //引入分页插件
        PageInterceptor interceptor = new PageInterceptor();//分页拦截对象
        //设置方言属性
        interceptor.setProperties(new Properties());
        factoryBean.setPlugins(interceptor);

        return factoryBean;
    }


    @Bean(name="druidStatInterceptor")//设置druid 的 aop切面类
    public DruidStatInterceptor getDruidStatInterceptor(){
        DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();
        return druidStatInterceptor;
    }


    @Bean//配置spring监控
    public BeanNameAutoProxyCreator getAutoProxyCreator(){
        BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
        beanNameAutoProxyCreator.setProxyTargetClass(true);
        beanNameAutoProxyCreator.setBeanNames(new String[]{"*Mapper","*Service"});
        beanNameAutoProxyCreator.setInterceptorNames("druidStatInterceptor");
        return beanNameAutoProxyCreator;
    }
}
